package com.wnm.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wnm.pojo.User;

@WebFilter({"/cart/*","/address/*","/order/*"})
public class UserFilter implements Filter {

	@Override
	public void destroy() {
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest _request = (HttpServletRequest) request;
		String uri = _request.getRequestURI();
		User user = (User) _request.getSession().getAttribute("user");

		// 判断_user是否为null
		if (user == null) {
			HttpServletResponse _response = (HttpServletResponse) response;
			_response.sendRedirect(_request.getContextPath() + "/pages/login.jsp");
		} else {
			chain.doFilter(_request, response);
		}
	}

}
